const App = getApp();
import util from '../../../utils/util.js'
import api from '../../../config/api.js'

const formatTime = util.formatTime;

Page({
  data: {
    trips: [],
    start: 0,
  },
  onReachBottom: function () {
    this.loadMore();
  },
  onLoad() {
    this.loadMore();
  },
  loadMore() {
    const self = this;
    wx.showToast({
      title: '正在加载',
      icon: 'loading'
    });
    util.request(api.hotTripList, { next_start: self.data.start}).then(function (res) {
      let newList = res.data.elements;
      newList.map((trip) => {
        const item = trip;
        item.data[0].date_added = formatTime(new Date(item.data[0].date_added * 1000), 1);
        return item;
      });
      newList = self.data.trips.concat(newList);
      const nextStart = res.data.next_start;
      self.setData({
        trips: newList,
        start: nextStart
      });
      wx.hideToast();
    });
  },
  viewTrip(e) {
    const ds = e.currentTarget.dataset;
    wx.navigateTo({
      url: `../tripdetail/tripdetail?id=${ds.id}&name=${ds.name}`,
    });
  },
});
